package com.example.myletcodelearing.designlinkedlist

/**
 * @author tgw
 * @date 2022/7/10
 * @describe
 */

fun main(array: Array<String>) {
    println("测试开始")
    val myLinkedList: MyLinkedList = MyLinkedList()
    myLinkedList.add(0)
    myLinkedList.add(1)

    val node = myLinkedList.get(0)
    println("测试开始数据： ${node?.value}")
}

class LinkedListNode {
    var value: Int = 0
    var next: LinkedListNode? = null
}

class MyLinkedList {

    private var list: LinkedListNode? = null

    fun add(value: Int) {
        if (list ==null){
            list =LinkedListNode()
        }

        while (list !=null && value>= list?.value!!) {

            if (list?.value == value){
              if (list?.next !=null){
                  val currentNode = list?.next
                  list?.next = LinkedListNode().apply {
                      this.value = value
                  }
                  list?.next?.next = currentNode
              }else{
                  list?.next = LinkedListNode().apply {
                      this.value = value
                  }
              }
                break
            }
            list =list?.next
        }

    }


    fun get(value: Int): LinkedListNode? {
        var node:LinkedListNode? = null
        while (list != null) {
            if (list?.value == value){
                node = list?.next
                break
            }
        }
        if (node==null){
            println("$value 查无此值")
        }
        return node
    }


}
